<?php


namespace app\mobile\service\base;


use app\admin\model\base\Student;
use app\admin\service\base\JwtService;
use app\BaseService;
use ciphertext\RsaPassword;
use think\response\Json;

class StudentService extends BaseService
{
    /**
     * 班级列表
     * @param array $params
     * @return Json
     */
    public function index(array $params): Json
    {
        $where = ['is_del'=>0];
        //获取列表
        $data = Student::getList($where, $params['page'] ?? 1, $params['limit'] ?? 10, 'id desc');
        return jsonReturn(1, $data);
    }

    /**
     * 详情
     * @param array $params
     * @return Json
     */
    public function read(array $params): Json
    {
        $where = ['id'=>MOBILE_USER_ID];
        $data = Student::where($where)->with(['special','grades','classes'])->field('username,realname,sex,special_id,status,grades_id,classes_id')->find();
        return jsonReturn(1, $data);
    }

    /**
     * 保存班级
     * @param array $params
     * @return Json
     */
    public function save(array $params): Json
    {
        if(isset($params['password']) && $params['password']){
            $params['password'] = md5($params['password']);
        }
        $data = Student::create([
            'username'=>$params['username'],
            'password'=>$params['password'],
            'realname'=>$params['realname'],
            'sex'=>$params['sex'],
            'status'=>$params['status'],
            'grades'=>$params['grades']
        ]);
        if($data){
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }

    /**
     * 编辑班级
     * @param array $params
     * @return Json
     */
    public function update(array $params): Json
    {
        if(isset($params['password']) && $params['password']){
            $params['password'] = md5($params['password']);
        }
        $data = Student::where('id','=',$params['id'])->update(
            [
                'username'=>$params['username'],
                'password'=>$params['password'],
                'realname'=>$params['realname'],
                'sex'=>$params['sex'],
                'grades'=>$params['grades']
            ]
        );
        if($data){
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }

    /**
     * 编辑班级
     * @param array $params
     * @return Json
     */
    public function delete(array $params): Json
    {
        $where = [];
        //获取列表
        $data = Student::where('id','=',$params['id'])->update(['is_del'=>1]);
        if($data){
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }

    /**
     * 管理员登录
     * @param int $phone
     * @param string $password
     * @param string $platform
     * @return Json
     */
    public function login(string $phone, string $password, string $platform): Json
    {
        //查询
        $user = Student::findOne(['username' => $phone, 'status' => 0, 'is_del' => 0 ]);
        if (!$user) {
            return jsonReturn(0, [], '账号不存在');
        }
        //先解密密码
//        $password = RsaPassword::privateDecrypt($password);
        //验证账号密码
        if (!password_verify($password . $user['safe_code'], $user['password'])) {
            return jsonReturn(0, ['err' => '密码不正确'], '密码不正确');
        }
        //更新数据库
        Student::update(['login_ip' => request()->ip(), 'login_time' => date('Y-m-d H:i:s',time())], ['id' => $user['id']]);
        //登录程序
        $jst_data = JwtService::authorizations($platform, intval($user['id']));
        if (!$jst_data['bool']) {
            return jsonReturn(0, [], '签发token失败');
        }
        return jsonReturn(1, [], '登录成功');
    }
}